﻿using System;
using System.Data;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Xml;
using System.Resources;
using EnvDTE;
using System.ComponentModel;
using System.Reflection;

namespace SPVisualDev
{
    internal struct ResourceKeyInfo
    {
        public string Path;
        public ProjectItem ProjectItem;
        public bool IsFeatureLocal;
        public string ResourceKey;
    }

    internal class ResourceStringContainer
    {
        Dictionary<string, bool> m_culture_strings_changes_map = new Dictionary<string, bool>();
        Dictionary<string, string> m_culture_to_string_map = new Dictionary<string, string>();
        ProjectItem m_parent_feature_folder;
        string m_default_resource_file;

        public ResourceStringContainer(string RawString, string DefaultResourceFile, ProjectItem ParentFeatureFolder)
        {
            this.RawString = RawString;

            m_default_resource_file = DefaultResourceFile;
            m_parent_feature_folder = ParentFeatureFolder;
        }

        private string m_raw_string = null;
        public string RawString
        {
            get
            {
                return m_raw_string;
            }
            set
            {
                m_raw_string = value;

                this.InvalidateCahe();
            }
        }

        public string DefaultResourceFile
        {
            get { return m_default_resource_file;  }
            set { m_default_resource_file = value; }
        }

        public bool IsResourceReadOnly
        {
            get;
            private set;
        }

        public ProjectItem ParentFeatureFolder
        {
            get { return m_parent_feature_folder; }
        }

        public bool HasUnsavedChanges 
        {
            get
            {
                return m_culture_strings_changes_map.ContainsValue(true);
            }
            set
            {
                if (value == false)
                {
                    m_culture_strings_changes_map = new Dictionary<string, bool>();
                }
                else
                {
                    m_culture_strings_changes_map = new Dictionary<string, bool>();
                    foreach (string s in m_culture_to_string_map.Keys)
                    {
                        m_culture_strings_changes_map.Add(s, true);
                    }
                }
            }
        }

        public IDictionary<string, string> GetAllCultureValues(bool ChangesOnly)
        {
            Dictionary<string, string> ret = new Dictionary<string, string>();
            
            foreach (KeyValuePair<string, string> kvp in m_culture_to_string_map)
            {
                if (ChangesOnly)
                {
                    if (m_culture_strings_changes_map.ContainsKey(kvp.Key))
                    {
                        if (m_culture_strings_changes_map[kvp.Key] != true)
                            continue;
                    }
                    else
                    {
                        continue;
                    }
                }

                ret.Add(kvp.Key, kvp.Value);
            }

            return ret;
        }


        public string GetValue(string Culture)
        {
            string ret = null;
            if (Culture == null)
                Culture = string.Empty;

            if (!ResourceHelper.IsResourceString(this.RawString))
                return this.RawString;

            if (!m_culture_to_string_map.ContainsKey(Culture))
            {
                ResourceKeyInfo key_info = ResourceHelper.ResolveRawStringDefinition(this.RawString, m_default_resource_file, m_parent_feature_folder);

                this.IsResourceReadOnly = (key_info.ProjectItem == null);

                if (!string.IsNullOrEmpty(key_info.Path))
                {
                    string dir = Path.GetDirectoryName(key_info.Path);
                    string res_file = Path.GetFileNameWithoutExtension(key_info.Path);
                    if (Culture != string.Empty)
                        res_file += "." + Culture + ".resx";
                    else
                        res_file += ".resx";

                    if (File.Exists(dir + "\\" + res_file))
                    {
                        using (ResXResourceSet set = new ResXResourceSet(dir + "\\" + res_file))
                        {
                            ret = set.GetString(key_info.ResourceKey);
                        }

                        m_culture_to_string_map.Add(Culture, ret);
                    }
                }
            }
            else
            {
                ret = m_culture_to_string_map[Culture];
            }

            return ret;
        }

        public void SetValue(string Culture, string Value)
        {
            if (Culture == null)
                Culture = string.Empty;

            if (!m_culture_to_string_map.ContainsKey(Culture))
                m_culture_to_string_map.Add(Culture, Value);
            else
                m_culture_to_string_map[Culture] = Value;

            
            //this.HasUnsavedChanges = true;

            if (!m_culture_strings_changes_map.ContainsKey(Culture))
            {
                m_culture_strings_changes_map.Add(Culture, true);
            }
            else
            {
                m_culture_strings_changes_map[Culture] = true;
            }
        }

        public override string ToString()
        {
            return this.RawString;
        }

        public void InvalidateCahe()
        {
            m_culture_to_string_map = new Dictionary<string, string>();

            this.HasUnsavedChanges = false;
        }
    }
}
